package com.cxd.batch;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import bank.yy.utils.JdbcUtils;

import com.alibaba.fastjson.JSONObject;
import com.cxd.batch.util.YeePayUtil;
/**
 * 解绑银行卡 快捷解绑定时
 * @author wtj
 * @date 2015年12月24日 下午3:26:21
 * @company CXD
 * @version [Copyright (c) 2015 V001]
 */
public class MainTask {
	
	private static JdbcUtils jdbc = new JdbcUtils();
	private static Connection conn = jdbc.getConnection();
	private static QueryRunner runner = new QueryRunner();
	@SuppressWarnings("rawtypes")
	private static ResultSetHandler handler = new MapListHandler();
	
	private static final String DEL_STR = "已删除";
	private static final String TO_STATUS = "un_card_ing";// "passed"
	public static void main(String[] arg){
		MainTask mt = new MainTask();
		// 解绑银行卡
		mt.unBankCard();
		
		// 实名认证回调
		mt.realnameAuthenticationCallBack();
	}
	
	// 实名认证回调
	public void realnameAuthenticationCallBack(){
		String sql = "select id,mark_id from trusteeship_operation where type = 'create_account' and status = 'sended'";
		List<Map<String, Object>> toList = new ArrayList<Map<String, Object>>();
		try {
			toList = (List) runner.query(conn, sql.toString(), handler);
			System.out.print("查询结果:"+JSONObject.toJSON(toList));
		} catch (SQLException e1) {
			e1.printStackTrace();
		}
		for(Map<String,Object> map:toList){
			String userId = map.get("mark_id").toString();
			String id = map.get("id").toString();
			try {
				Map<String,Object> yeePayUserMap = YeePayUtil.queryYeePayUserInfo(userId);
				
				
				if(yeePayUserMap.get("code")!=null
						&&yeePayUserMap.get("code").equals("1")){
					runner.update(conn,"INSERT INTO `chengxindai`.`trusteeship_account` (`id`, `account_id`, `create_time`, `status`, `trusteeship`, `user_id`) VALUES ('"+userId+"', '"+userId+"', now(), 'passed', 'yeepay', '"+userId+"')");
					runner.update(conn,"INSERT INTO `chengxindai`.`user_role` (`user_id`, `role_id`) VALUES ('"+userId+"', 'INVESTOR')");
					runner.update(conn,"update trusteeship_operation set status='passed' where id='"+id+"'");
					System.out.print("用户【"+userId+"】易宝开户成功");
				}
			} catch (Exception e) {
				if(e.getMessage().indexOf("PRIMARY")>0){
					try {
						runner.update(conn,"update trusteeship_operation set status='passed' where id='"+id+"'");
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}
			}
		}
		try {
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 解绑银行卡
	 * <一句话功能简述> <功能详细描述>
	 * @author wtj
	 * @date 2016年1月27日下午1:49:47
	 */
	public void unBankCard(){
		StringBuffer sql = new StringBuffer("select DISTINCT t.mark_id from"
				+ " ("
				+ " 	select too.mark_id from trusteeship_operation too where too.`status` = '"+TO_STATUS+"' and now() > date_add(response_time, interval 24 hour)"
				+ " 	union all"
				+ " 	SELECT user_id as mark_id from bank_card where status = '解绑审核中'"
				+ " ) t");
		
		System.out.println("查询sql:"+sql);
		
			List<Map<String, Object>> toList = new ArrayList<Map<String, Object>>();
			try {
				toList = (List) runner.query(conn, sql.toString(), handler);
				System.out.print("查询结果:"+JSONObject.toJSON(toList));
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			for(Map<String,Object> map:toList){
				try {
					String userId = map.get("mark_id").toString();
					Map<String,Object> yeePayUserMap = YeePayUtil.queryYeePayUserInfo(userId);
					if(yeePayUserMap.get("code")!=null
							&&yeePayUserMap.get("code").equals("1")
								&& (yeePayUserMap.get("cardNo")==null || yeePayUserMap.get("cardNo")=="")){
						runner.update(conn,"update bank_card set status='"+DEL_STR+"' ,time = now() where user_id='"+userId+"'");
						runner.update(conn,"update trusteeship_operation set status='passed',response_time=now() where mark_id='"+userId+"'");
						System.out.print("用户【"+userId+"】解绑成功");
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			try {
				conn.commit();
			} catch (SQLException e) {
				e.printStackTrace();
			}
	}
}
